package com.template;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import com.template.utils.TStringUtils;

public class TEntity {

	public static void createXMLFile(String tableName, String packageName,List<TableBean>columnList,List<TableBean>priList, List<TableBean>noPriList, String folderName, String path) {
		String className = TStringUtils.tableName2ClassName(tableName);
	    String attrName = TStringUtils.columnName2Attr(tableName);
	    String str = "";
	    str +="package "+packageName+".entity;\r\n";
	    str +="public class "+className+" {\r\n";

		/**
		 * 生成属性
		 */
		for (int i = 0; i < columnList.size(); i++) {
			TableBean tBean = columnList.get(i);
			str += "\r\n";
			str += "	/**\r\n";
			str += "	 * " + tBean.getColumnComment() + " \r\n";
			str += "	*/\r\n";
			if ("int".equals(tBean.getDataType())|| "bigint".equals(tBean.getDataType())) {
				str += "	private Long "
						+ TStringUtils.columnName2Attr(tBean.getColumnName())
						+ ";\r\n";
			} else if ("date".equals(tBean.getDataType())
					|| "datetime".equals(tBean.getDataType())
					|| "timestamp".equals(tBean.getDataType())) {
				str += "	private Date "
						+ TStringUtils.columnName2Attr(tBean.getColumnName())
						+ ";\r\n";
			} else {
				str += "	private String "
						+ TStringUtils.columnName2Attr(tBean.getColumnName())
						+ ";\r\n";
			}
		}
	    
		/**
		 * 生成数据库对应方法
		 */
		for (int i = 0; i < columnList.size(); i++) {
			TableBean tBean = columnList.get(i);
			String type = "String";
			if ("int".equals(tBean.getDataType())) {
				type = "Long";
			} else if ("date".equals(tBean.getDataType())
					|| "datetime".equals(tBean.getDataType())
					|| "timestamp".equals(tBean.getDataType())) {
				type = "Date";
			}
			str += "    	\r\n";
			str += "    /** \r\n";
			str += "     * " + tBean.getColumnComment() + "\r\n";
			str += "     * @return \r\n";
			str += "     */ \r\n";
			str += "    public " + type + " get"
					+ TStringUtils.tableName2ClassName(tBean.getColumnName())
					+ "() {\r\n";
			str += "    	return "
					+ TStringUtils.columnName2Attr(tBean.getColumnName())
					+ ";\r\n";
			str += "    }\r\n";

			str += "    \r\n";
			str += "    /** \r\n";
			str += "     * " + tBean.getColumnComment() + "\r\n";
			str += "     * @param \r\n";
			str += "     */ \r\n";
			str += "    public void set"
					+ TStringUtils.tableName2ClassName(tBean.getColumnName())
					+ "(" + type + " "
					+ TStringUtils.columnName2Attr(tBean.getColumnName())
					+ ") {\r\n";
			str += "    	this."
					+ TStringUtils.columnName2Attr(tBean.getColumnName())
					+ " = "
					+ TStringUtils.columnName2Attr(tBean.getColumnName())
					+ ";\r\n";
			str += "    }\r\n";
		}
		
		str +="@Override\r\n";
		str +="public String toString() {\r\n";
		str +="return \""+className+" [";
		for(int i=0;i<columnList.size();i++){
		str +=columnList.get(i).getColumnName()+"=\" + "+ TStringUtils.columnName2Attr(columnList.get(i).getColumnName())+" + \""+ (i==columnList.size()-1?"]\";\r\n":",");	
		}
		str +="  }\r\n\r\n";
		str +="}\r\n";
		
		try {
			String allPath = path+"\\"+className+".java";
			OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(allPath),"UTF-8");
			output.write(str);
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
